package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.Client;
import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.domain.TransferHistory;
import cn.wolfcode.edu.mapper.ClientMapper;
import cn.wolfcode.edu.mapper.EmployeeMapper;
import cn.wolfcode.edu.mapper.TransferHistoryMapper;
import cn.wolfcode.edu.query.ClientQueryObject;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.service.IClientService;
import cn.wolfcode.edu.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {

    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private TransferHistoryMapper transferHistoryMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    public PageResult query(ClientQueryObject qo) {
        Long employeeId = null;

        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        //判断是不是超级管理员
        if (!employee.isAdmin()) {  //不是超级管理员
            //判断是不是市场经理
            if (!subject.hasRole("MARKMGR")) {  //不是市场经理
                employeeId = employee.getId();
                qo.setEmployeeId(employeeId);
            }
        }

        //先查询数据条数
        Integer total = clientMapper.queryForCount(qo);

        //查询客户数据集
        List<Client> rows =  clientMapper.queryForList(qo);
        return new PageResult(total,rows);
    }


    public void save(Client client) {
        //1.填入录入人(当前登录用户)
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        client.setInputUser(currentUser);
        //2.填入录入时间(当前世界)
        client.setInputTime(new Date());
        //3.填入第一的创建的最后跟踪时间为当前世界
        client.setLastTraceTime(new Date());
        clientMapper.insert(client);
    }

    public void edit(Client client) {
        clientMapper.updateByPrimaryKey(client);
    }


    public Client getClientById(Long clientId) {
        Client client = clientMapper.selectByPrimaryKey(clientId);
        return client;
    }


    public void abandon(Long id) {
        //得到当前登录的用户
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //得到当前移入资源池的对象
        Client client = clientMapper.selectByPrimaryKey(id);
        TransferHistory transferHistory = new TransferHistory(new Date(), employee.getRealName(), null, client);
        //保存移交日子
        transferHistoryMapper.insert(transferHistory);
        clientMapper.abandon(id);
    }


    public void joinMe(Long clientId) {
        //得到当前登陆用的Id
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        clientMapper.joinMe(clientId,employee.getId());
        //移交历史 设置移交目标
        transferHistoryMapper.setTarget(employee.getRealName(),clientId);
    }

    public PageResult listNoSaleman(ClientQueryObject qo) {
        //查询数据数
        Integer total = clientMapper.queryForCountNoSaleman(qo);
        //查询数据集
        List rows =  clientMapper.listNoSaleman(qo);
        return new PageResult(total,rows);
    }


    public void dispatch(Long salemanId,Long clientId) {
        //将资源池中的潜在客户指派给指定ID的员工
        clientMapper.joinMe(clientId,salemanId);
        Employee employee = employeeMapper.selectByPrimaryKey(salemanId);
        //移交历史 设置移交目标
        transferHistoryMapper.setTarget(employee.getRealName(),clientId);
    }

    public List queryForList(ClientQueryObject qo) {
        return clientMapper.queryForList(qo);
    }
}
